/* 
 * Fichier javascript pour le bloc image
 * dans Fo : ajout annonnce
 */


     $(function(){
          
         item = '';
         delSrc = '';
         noImage = $('#noImage').val();
         path = $('#path').val();;
        
            $('#status').hide();
            initCarousel();
            //initGallery();
            delSrc = $('#delImage').val();
            $('#thumb').bind('click', function() {
                var src = $(this).attr('src');
                var tabSrc = src.split('/');
                if(tabSrc[tabSrc.length - 1] != 'no_image.jpg'){
                    $('#ap_img').fadeOut(300, function() {
                        $(this).attr('src',src);
                    }).fadeIn(300);
                    initPP(src);
                }
                //majCoordShowPP($(this).height());
                
            });
            
        });
        
        function countImage () {
            nbr = 0;
            if ($.trim($('.infiniteCarousel ul').html()) == "") return 0;
            $('.infiniteCarousel li').each(function () {
                cl = $(this).attr('class');
                if(cl && (cl.indexOf('empty') == -1) && (cl.indexOf('cloned') == -1) ) nbr++;
            });

            return nbr + 1;
        }
        
        

        function searchInArray(val,tab){
            var len = tab.length;
            for(i=0;i<len;i++){
                if(tab[i] == val) {
                    return true; 
                }
            }return false;
        }
        
        
        
       function hideOrShowIfEmpty(ul,div){
           var contenu = $.trim($(ul).html());
            if (contenu == ""){
                $(div).hide();
                $('#fic').val('');
                $('#thumb').attr('src','');
            } else {
                $(div).show();
            }
       }
        
        function addImageinCarousel(url){
			var zUrlImage = decodeURI(url) ;
            delSrc = $('#delImage').val(); 
            var name = zUrlImage.split("/");
			var zBasePathName = $("#zBasePath").val() ;
			var zImage = img_path ;
            //var name = url.split("///");
            $('#fic').val(zUrlImage);
            $('#thumb').attr('src',zUrlImage);
            var tab = new Array(10);
            var html  = '<div class="wrapper"><ul>';
            var cptr = 1;
            var photo_annonce = $('#photo-annonce').val();
            $('.infiniteCarousel li img:not(.deleteImage)').each(function(){
                var src2 = $(this).attr('src');
                if (!searchInArray(src2,tab)) {
                    tab.push(src2);
                    cptr++;
                    html += '<li><img src="'+$(this).attr('src')+'" ';
                    html += 'title="'+$(this).attr('src')+'" width="84" /><img src="'+delSrc+'" class="deleteImage" title="Supprimer cette image" /></li>';
                }
            });
            html += '<li><img src="'+zUrlImage+'"  title="' + zUrlImage+'"  width="84" /><img src="'+delSrc+'" class="deleteImage" title="Supprimer cette image" /></li></ul></div>';

           // photo_annonce += (photo_annonce == '') ? name[name.length -1] : ',' + name[name.length -1];
            photo_annonce += (photo_annonce == '') ? zUrlImage : ',' + zUrlImage;
            $('#photo-annonce').val(photo_annonce);
            $('.infiniteCarousel').html(html);
            $('.infiniteCarousel').show();
            initCarousel();
        }
        
        function initCarousel(){
            
            $('.infiniteCarousel').infiniteCarousel();
            hideOrShowIfEmpty('.infiniteCarousel ul','.infiniteCarousel');
                
            $('.deleteImage').click(function(){
                
                var toDel = $(this).prev().attr('src');
                
                var tab = new Array(10);
                var appercu = '';
                var forPop = '';
                var html  = '<div class="wrapper"><ul>';
                var photo_annonce = '';
                h = 0;
                $('.infiniteCarousel li img:not(.deleteImage)').each(function(){
                    var src2 = $(this).attr('src');
                    if ( (src2 != toDel) &&(!searchInArray(src2,tab))) {
                        tab.push(src2);
                        var tabSrc = src2.split('/');
                        appercu = $(this).attr('src')
                        h = $(this).height();
                        html += '<li><img src="'+src2+'" ';
                        photo_annonce += (photo_annonce == '') ? tabSrc[tabSrc.length -1] : ',' +  tabSrc[tabSrc.length -1];
                        html += 'title="'+src2+'" width="84" /><img src="' + delSrc +'" class="deleteImage" title="Supprimer cette image" /></li>';
                        
                    }
                });
                $('#photo-annonce').val(photo_annonce);
                html += '</ul></div>';
                $('.infiniteCarousel').html(html);
                initCarousel();
            });
        }

